package com.cms.util;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;

import javax.servlet.http.HttpServletRequest;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class FileHelper {
	public static String FILE_URL_BASE = "/uploadfiles";
	public static String FILE_NEWS_IMG_URL = FILE_URL_BASE + "/newsimg/";
	public static String FILE_ATTACHMENT_URL = FILE_URL_BASE + "/attachment/";
	public static String FILE_RSF_HTML_URL =  FILE_URL_BASE + "/rsfhtml/";
	public static String FILE_CKEDITOR_IMG_URL = FILE_URL_BASE + "/ckeditorimg/";
	public static String FILE_ATTACHMENT_RSF_URL = FILE_ATTACHMENT_URL + "rsf/";
	public static String FILE_PRODUCT_URL = FILE_URL_BASE + "/product/";
	public static String FILE_PRODUCT_COVER_URL = FILE_URL_BASE + "cover/";
	public static String FILE_PRODUCT_PICTURES_URL = FILE_URL_BASE + "pictures/";
	public static String FILE_PRODUCT_HTML_URL = FILE_URL_BASE + "html/";
	
	
	public static String FILE_PATH_BASE = "";
	public static String FILE_NEWS_IMG_PATH = FILE_PATH_BASE + "/newsimg/";
	public static String FILE_ATTACHMENT_PATH = FILE_PATH_BASE + "/attachment/";
	public static String FILE_TEMP_PATH = FILE_PATH_BASE + "/temp/";
	public static String FILE_RSF_HTML_PATH =  FILE_PATH_BASE + "/rsfhtml/";
	public static String FILE_CKEDITOR_IMG_PATH = FILE_PATH_BASE + "/ckeditorimg/";
	public static String FILE_ATTACHMENT_RSF_PATH = FILE_ATTACHMENT_PATH + "rsf/";
	public static String FILE_PRODUCT_PATH = FILE_PATH_BASE + "/product/";
	public static String FILE_PRODUCT_COVER_PATH = FILE_PRODUCT_PATH + "cover/";
	public static String FILE_PRODUCT_PICTURES_PATH = FILE_PRODUCT_PATH + "pictures/";
	public static String FILE_PRODUCT_HTML_PATH = FILE_PRODUCT_PATH + "html/";
	public static void initPathDIR(String basePath){
		FILE_PATH_BASE = basePath;
		FILE_NEWS_IMG_PATH = FILE_PATH_BASE + "/newsimg/";
		FILE_ATTACHMENT_PATH = FILE_PATH_BASE + "/attachment/";
		FILE_TEMP_PATH = FILE_PATH_BASE + "/temp/";
		FILE_RSF_HTML_PATH =  FILE_PATH_BASE + "/rsfhtml/";
		FILE_CKEDITOR_IMG_PATH = FILE_PATH_BASE + "/ckeditorimg/";
        FILE_ATTACHMENT_RSF_PATH = FILE_ATTACHMENT_PATH + "rsf/";
    	FILE_PRODUCT_PATH = FILE_PATH_BASE + "/product/";
    	FILE_PRODUCT_COVER_PATH = FILE_PRODUCT_PATH + "cover/";
    	FILE_PRODUCT_PICTURES_PATH = FILE_PRODUCT_PATH + "pictures/";
    	FILE_PRODUCT_HTML_PATH = FILE_PRODUCT_PATH + "html/";
	}
	
	/**
	 * 根据ID获得产品简介HTML相对路径
	 * @param infoId
	 * @return
	 */
	public static String getProductContentHTMLURL(int infoId){
		StringBuffer sb = new StringBuffer(FILE_PRODUCT_HTML_URL);
		int below = (int) (infoId / 100) * 100;
		int above = ((int) (infoId / 100) + 1) * 100;
		sb.append(below + "-" + above + "/");
		sb.append(infoId);
		sb.append(".html");
		return sb.toString();
	}
	/**
	 * 根据ID获得产品简介html
	 * @param infoId
	 * @return
	 */
	public static String getProductContentHTMLPath(int infoId){
		StringBuffer sb = new StringBuffer(FILE_PRODUCT_HTML_PATH);
		int below = (int) (infoId / 100) * 100;
		int above = ((int) (infoId / 100) + 1) * 100;
		sb.append(below + "-" + above + "/");
		File checkDir = new File(sb.toString());
		if (!checkDir.isDirectory())
			checkDir.mkdirs();
		sb.append(infoId);
		sb.append(".html");
		return sb.toString();			
	}
	/**
	 * 产品封面图片路径
	 * @param infoId
	 * @param fileName
	 * @return
	 */
	public static String getProductCoverPath(int infoId,String fileName){
		StringBuffer sb = new StringBuffer(FILE_PRODUCT_COVER_PATH);
		int below = (int) (infoId / 100) * 100;
		int above = ((int) (infoId / 100) + 1) * 100;
		sb.append(below + "-" + above + "/");
		File checkDir = new File(sb.toString());
		if (!checkDir.isDirectory())
			checkDir.mkdirs();
		sb.append(infoId);
		sb.append(".");
		sb.append(fileName.substring(fileName.lastIndexOf(".")));
		return sb.toString();	
	}
	
	/**
	 * 产品图片路径
	 * @param infoId
	 * @param fileName
	 * @return
	 */
	public static String getProductPicturesPath(int infoId,int picture_id,String fileName){
		StringBuffer sb = new StringBuffer(FILE_PRODUCT_PICTURES_PATH);
		int below = (int) (infoId / 100) * 100;
		int above = ((int) (infoId / 100) + 1) * 100;
		sb.append(below + "-" + above + "/");
		File checkDir = new File(sb.toString());
		if (!checkDir.isDirectory())
			checkDir.mkdirs();
		sb.append(infoId);
		sb.append("_");
		sb.append(picture_id);
		sb.append(".");
		sb.append(fileName.substring(fileName.lastIndexOf(".")));
		return sb.toString();	
	}
	
	
	public static String getNewImgFilePath(int infoId,String fileName) {
		StringBuffer sb = new StringBuffer(FILE_NEWS_IMG_PATH);
		int below = (int) (infoId / 100) * 100;
		int above = ((int) (infoId / 100) + 1) * 100;
		sb.append(below + "-" + above + "/");
		File checkDir = new File(sb.toString());
		if (!checkDir.isDirectory())
			checkDir.mkdirs();
		sb.append(infoId);
		sb.append(fileName.substring(fileName.lastIndexOf(".")));
		return sb.toString();
	}

	public static String getNewImgFileURL(int infoId,String fileName) {
		StringBuffer sb = new StringBuffer(FILE_NEWS_IMG_URL);
		int below = (int) (infoId / 100) * 100;
		int above = ((int) (infoId / 100) + 1) * 100;
		sb.append(below + "-" + above + "/");
		File checkDir = new File(sb.toString());
		if (!checkDir.isDirectory())
			checkDir.mkdirs();
		sb.append(infoId);
		sb.append(".");
		sb.append(fileName.substring(fileName.lastIndexOf(".")));
		return sb.toString();
	}
	
	public static String getAttachmentPath(int reqId) {
		StringBuffer sb = new StringBuffer(FILE_ATTACHMENT_PATH);
		int below = (int) (reqId / 100) * 100;
		int above = ((int) (reqId / 100) + 1) * 100;
		sb.append(below + "-" + above + "/");
		File checkDir = new File(sb.toString());
		if (!checkDir.isDirectory())
			checkDir.mkdir();
		sb.append(reqId);

		return sb.toString();
	}
	
	/**
	 * 获得RSF文件相对路径
	 * @param rsf_id
	 * @return
	 */
	public static String getRSFContentHtmlURL(int rsf_id){
		StringBuffer sb = new StringBuffer(FILE_RSF_HTML_URL);
		int below = (int) (rsf_id / 100) * 100;
		int above = ((int) (rsf_id / 100) + 1) * 100;
		sb.append(below + "-" + above + "/");
		File checkDir = new File(sb.toString());
		if (!checkDir.isDirectory())
			checkDir.mkdir();
		sb.append(rsf_id);
		sb.append(".html");
		return sb.toString();
	}
	
	/**
	 * 获得RSF文件绝对路径
	 * @param rsf_id
	 * @return
	 */
	public static String getRSFContentHtmlPath(int rsf_id){
		StringBuffer sb = new StringBuffer(FILE_RSF_HTML_PATH);
		int below = (int) (rsf_id / 100) * 100;
		int above = ((int) (rsf_id / 100) + 1) * 100;
		sb.append(below + "-" + above + "/");
		File checkDir = new File(sb.toString());
		if (!checkDir.isDirectory())
			checkDir.mkdir();
		sb.append(rsf_id);
		sb.append(".html");
		return sb.toString();	
	}
	
	/**
	 * RSF附件绝对路径
	 * @param rsf_id
	 * @param attach_id
	 * @param fileName
	 * @return
	 */
	public static String getRSFAttachmentPath(int rsf_id,int attach_id,String fileName){
		StringBuffer sb = new StringBuffer(FILE_ATTACHMENT_RSF_PATH);
		int below = (int) (rsf_id / 100) * 100;
		int above = ((int) (rsf_id / 100) + 1) * 100;
		sb.append(below + "-" + above + "/");
		sb.append(rsf_id);
		sb.append("/");
		File checkDir = new File(sb.toString());
		if (!checkDir.isDirectory())
			checkDir.mkdirs();
		sb.append(rsf_id);
		sb.append("_");
		sb.append(attach_id);
		sb.append(fileName.substring(fileName.lastIndexOf(".")));
		return sb.toString();
	}
	
	/**
	 * RSF附件相对路径
	 * @param rsf_id
	 * @param attach_id
	 * @param fileName
	 * @return
	 */
	public static String getRSFAttachmentURL(int rsf_id,int attach_id,String fileName){
		StringBuffer sb = new StringBuffer(FILE_ATTACHMENT_RSF_URL);
		int below = (int) (rsf_id / 100) * 100;
		int above = ((int) (rsf_id / 100) + 1) * 100;
		sb.append(below + "-" + above + "/");
		sb.append(rsf_id);
		sb.append("/");
		sb.append(rsf_id);
		sb.append("_");
		sb.append(attach_id);
		sb.append(fileName.substring(fileName.lastIndexOf(".")));
		return sb.toString();
	}
	
	public static void writeRSFContentFile(String content, String file_path) {

		PrintStream p = null;
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(file_path);
			p = new PrintStream(out);
			p.print(content);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				p.close();
				out.close();
			} catch (Exception e1) {
			}
		}
	}
	
	/**
	 * 根据文件路径读取文件内容
	 * @param path
	 * @return
	 */
	@SuppressWarnings("finally")
	public static String readFile(String path) {
		String result = "";
		FileInputStream fis = null;
		BufferedReader reader = null;
		InputStreamReader read = null;
		try {
			File f = new File(path);
			fis = new FileInputStream(f);

			read = new InputStreamReader(fis);
			reader = new BufferedReader(read);
			String line;
			while ((line = reader.readLine()) != null) {
				result += line;
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (Exception e1) {
			}
			try {
				read.close();
			} catch (Exception e1) {
			}
			try {
				fis.close();
			} catch (Exception e1) {
			}
			return result;
		}
	}
	
	public static String getCkeditorImgPath(String fileName) {
		StringBuffer sb = new StringBuffer(FILE_CKEDITOR_IMG_PATH);
		sb.append(fileName);
		return sb.toString();
	}
	
	public static String getCkeditorImgURL(String fileName) {
		StringBuffer sb = new StringBuffer(FILE_CKEDITOR_IMG_URL);
		sb.append(fileName);
		return sb.toString();
	}
	
	public static String getWebappsPath(HttpServletRequest request){
		String projectPath = request.getSession().getServletContext().getRealPath("/");
		return projectPath;
	}
	
	public static void reduceImg(String imgsrc, String imgdist) {
		try {
			File srcfile = new File(imgsrc);
			if (!srcfile.exists()) {
				return;
			}
			int widthdist = 0;
			int heightdist = 0;
			Image src = javax.imageio.ImageIO.read(srcfile);

			int imageWidth = src.getWidth(null);
			int imageHeight = src.getHeight(null);

			if (imageWidth > 900 || imageHeight > 900) {
				if (imageWidth > imageHeight
						|| (imageWidth > 900 && imageHeight < 900)) {
					widthdist = 900;
					heightdist = (int) Math.round(widthdist * imageHeight
							* 1.00 / imageWidth);
				} else if (imageWidth < imageHeight
						|| (imageWidth < 900 && imageHeight > 900)) {
					heightdist = 900;
					widthdist = (int) Math.round(heightdist * imageWidth * 1.00
							/ imageHeight);
				}
			}else{
				return;
			}

			BufferedImage tag = new BufferedImage((int) widthdist,
					(int) heightdist, BufferedImage.TYPE_INT_RGB);

			tag.getGraphics().drawImage(
					src.getScaledInstance(widthdist, heightdist,
							Image.SCALE_SMOOTH), 0, 0, null);
			// tag.getGraphics().drawImage(src.getScaledInstance(widthdist,
			// heightdist, Image.SCALE_AREA_AVERAGING), 0, 0, null);

			FileOutputStream out = new FileOutputStream(imgdist);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			encoder.encode(tag);
			out.close();

		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
	
	public static void ImgOp(String folder){
		File fl = new File(folder);
		if(fl.isDirectory()){
			File[] files = fl.listFiles();
			for(int i=0;i<files.length;i++){
				String path = files[i].toString();
				String folderpath = path.substring(0, path.lastIndexOf("\\"));
				String fileName = path.substring(path.lastIndexOf("\\")+1);
				System.out.println("---"+fileName);
				String new_file_name = fileName.substring(0,fileName.lastIndexOf("."))+"_new"+fileName.substring(fileName.lastIndexOf("."));
			
				reduceImg(path,folderpath+"\\"+new_file_name);
			}
		}
	}
}
